home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / CmdDlgOneParam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-05  |  9.6 KB  |  328 lines  |  [TEXT/KAHL]

  1. /* CmdDlgOneParam.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "CmdDlgOneParam.h"
  31. #include "Memory.h"
  32. #include "Screen.h"
  33. #include "EventLoop.h"
  34. #include "Menus.h"
  35. #include "TextEdit.h"
  36. #include "SimpleButton.h"
  37. #include "WrapTextBox.h"
  38. #include "DataMunging.h"
  39. #include "Main.h"
  40. #include "Alert.h"
  41. #include "Numbers.h"
  42.  
  43.  
  44. #define WINXSIZE (350)
  45.  
  46. #define PROMPTX (10)
  47. #define PROMPTY (5)
  48. #define PROMPTWIDTH (WINXSIZE - (2 * PROMPTX))
  49. #define PROMPTHEIGHT (50)
  50.  
  51. #define BOXPROMPTX (PROMPTX)
  52. #define BOXPROMPTY (PROMPTY + PROMPTHEIGHT + 5)
  53.  
  54. #define BOXEDITWIDTH (140)
  55. #define BOXEDITHEIGHT (21)
  56. #define BOXEDITX (WINXSIZE - BOXEDITWIDTH - 10)
  57. #define BOXEDITY (BOXPROMPTY - 3)
  58.  
  59. #define CANCELBUTTONWIDTH (80)
  60. #define CANCELBUTTONHEIGHT (21)
  61. #define CANCELBUTTONX ((1 * WINXSIZE) / 4 - (CANCELBUTTONWIDTH / 2))
  62. #define CANCELBUTTONY (BOXEDITY + BOXEDITHEIGHT + 10)
  63.  
  64. #define OKBUTTONWIDTH (CANCELBUTTONWIDTH)
  65. #define OKBUTTONHEIGHT (CANCELBUTTONHEIGHT)
  66. #define OKBUTTONX ((3 * WINXSIZE) / 4 - (OKBUTTONWIDTH / 2))
  67. #define OKBUTTONY (CANCELBUTTONY)
  68.  
  69. #define WINYSIZE (CANCELBUTTONY + CANCELBUTTONHEIGHT + 20)
  70.  
  71.  
  72. typedef struct
  73.     {
  74.         WinType*                    ScreenID;
  75.         char*                            PromptText;
  76.         char*                            EditPromptText;
  77.         TextEditRec*            EditBox;
  78.         SimpleButtonRec*    OKButton;
  79.         SimpleButtonRec*    CancelButton;
  80.     } WindowRec;
  81.  
  82.  
  83. static void            RedrawWindow(WindowRec* Window)
  84.     {
  85.         CheckPtrExistence(Window);
  86.         TextEditFullRedraw(Window->EditBox);
  87.         RedrawSimpleButton(Window->OKButton);
  88.         RedrawSimpleButton(Window->CancelButton);
  89.         SetClipRect(Window->ScreenID,0,0,WINXSIZE,WINYSIZE);
  90.         DrawWrappedTextBox(Window->ScreenID,Window->PromptText,GetScreenFont(),9,
  91.             PROMPTX,PROMPTY,PROMPTWIDTH,PROMPTHEIGHT);
  92.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->EditPromptText,
  93.             StrLen(Window->EditPromptText),BOXPROMPTX,BOXPROMPTY,ePlain);
  94.     }
  95.  
  96.  
  97. /* present a dialog box that allows the user to edit a single parameter */
  98. /* returns True if the user changes the value and clicks OK. */
  99. MyBoolean                CommandDialogOneParam(char* Prompt, char* BoxName, double* DataInOut)
  100.     {
  101.         WindowRec*        Window;
  102.         char*                    StringTemp;
  103.         MyBoolean            LoopFlag;
  104.         MyBoolean            DoItFlag EXECUTE(= -31342);
  105.         MyBoolean            ReturnValue;
  106.  
  107.         Window = (WindowRec*)AllocPtrCanFail(sizeof(WindowRec),
  108.             "CommandDialogOneParam:  WindowRec");
  109.         if (Window == NIL)
  110.             {
  111.              FailurePoint1:
  112.                 AlertHalt("There is not enough memory available to edit the "
  113.                     "command parameters.",NIL);
  114.                 return False;
  115.             }
  116.         Window->PromptText = Prompt;
  117.         Window->EditPromptText = BoxName;
  118.  
  119.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  120.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WINXSIZE),
  121.             DialogTopEdge(WINYSIZE),WINXSIZE,WINYSIZE,(void (*)(void*))&RedrawWindow,Window);
  122.         if (Window->ScreenID == NIL)
  123.             {
  124.              FailurePoint2:
  125.                 ReleasePtr((char*)Window);
  126.                 goto FailurePoint1;
  127.             }
  128.         SetWindowName(Window->ScreenID,"Edit Command");
  129.  
  130.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKBUTTONX,OKBUTTONY,
  131.             OKBUTTONWIDTH,OKBUTTONHEIGHT);
  132.         if (Window->OKButton == NIL)
  133.             {
  134.              FailurePoint3:
  135.                 KillWindow(Window->ScreenID);
  136.                 goto FailurePoint2;
  137.             }
  138.         SetDefaultButtonState(Window->OKButton,True);
  139.  
  140.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELBUTTONX,
  141.             CANCELBUTTONY,CANCELBUTTONWIDTH,CANCELBUTTONHEIGHT);
  142.         if (Window->CancelButton == NIL)
  143.             {
  144.              FailurePoint4:
  145.                 DisposeSimpleButton(Window->OKButton);
  146.                 goto FailurePoint3;
  147.             }
  148.  
  149.         Window->EditBox = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),9,
  150.             BOXEDITX,BOXEDITY,BOXEDITWIDTH,BOXEDITHEIGHT);
  151.         if (Window->EditBox == NIL)
  152.             {
  153.              FailurePoint5:
  154.                 DisposeSimpleButton(Window->CancelButton);
  155.                 goto FailurePoint4;
  156.             }
  157.         StringTemp = LongDoubleToString(*DataInOut,12,1e-6,1e6);
  158.         if (StringTemp == NIL)
  159.             {
  160.              FailurePoint6:
  161.                 DisposeTextEdit(Window->EditBox);
  162.                 goto FailurePoint5;
  163.             }
  164.         TextEditNewRawData(Window->EditBox,StringTemp,SYSTEMLINEFEED);
  165.         ReleasePtr(StringTemp);
  166.         TextEditHasBeenSaved(Window->EditBox);
  167.         TextEditDoMenuSelectAll(Window->EditBox);
  168.  
  169.  
  170.         EnableTextEditSelection(Window->EditBox);
  171.         LoopFlag = True;
  172.         while (LoopFlag)
  173.             {
  174.                 OrdType                            X;
  175.                 OrdType                            Y;
  176.                 ModifierFlags                Modifiers;
  177.                 MenuItemType*                MenuItem;
  178.                 char                                KeyPress;
  179.  
  180.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  181.                     {
  182.                         default:
  183.                             break;
  184.                         case eCheckCursor:
  185.                             if (TextEditIBeamTest(Window->EditBox,X,Y))
  186.                                 {
  187.                                     SetIBeamCursor();
  188.                                 }
  189.                              else
  190.                                 {
  191.                                     SetArrowCursor();
  192.                                 }
  193.                             goto UpdateCursorPoint;
  194.                             break;
  195.                         case eNoEvent:
  196.                          UpdateCursorPoint:
  197.                             TextEditUpdateCursor(Window->EditBox);
  198.                             break;
  199.                         case eMenuStarting:
  200.                             EnableMenuItem(mPaste);
  201.                             if (TextEditIsThereValidSelection(Window->EditBox))
  202.                                 {
  203.                                     EnableMenuItem(mCut);
  204.                                     EnableMenuItem(mCopy);
  205.                                     EnableMenuItem(mClear);
  206.                                 }
  207.                             EnableMenuItem(mSelectAll);
  208.                             if (TextEditCanWeUndo(Window->EditBox))
  209.                                 {
  210.                                     EnableMenuItem(mUndo);
  211.                                 }
  212.                             break;
  213.                         case eMenuCommand:
  214.                             if (MenuItem == mPaste)
  215.                                 {
  216.                                     TextEditDoMenuPaste(Window->EditBox);
  217.                                 }
  218.                             else if (MenuItem == mCut)
  219.                                 {
  220.                                     TextEditDoMenuCut(Window->EditBox);
  221.                                 }
  222.                             else if (MenuItem == mCopy)
  223.                                 {
  224.                                     TextEditDoMenuCopy(Window->EditBox);
  225.                                 }
  226.                             else if (MenuItem == mClear)
  227.                                 {
  228.                                     TextEditDoMenuClear(Window->EditBox);
  229.                                 }
  230.                             else if (MenuItem == mUndo)
  231.                                 {
  232.                                     TextEditDoMenuUndo(Window->EditBox);
  233.                                     TextEditShowSelection(Window->EditBox);
  234.                                 }
  235.                             else if (MenuItem == mSelectAll)
  236.                                 {
  237.                                     TextEditDoMenuSelectAll(Window->EditBox);
  238.                                 }
  239.                             else
  240.                                 {
  241.                                     EXECUTE(PRERR(AllowResume,
  242.                                         "CommandDialogOneParam: Undefined menu option chosen"));
  243.                                 }
  244.                             break;
  245.                         case eKeyPressed:
  246.                             if (KeyPress == 13)
  247.                                 {
  248.                                     FlashButton(Window->OKButton);
  249.                                     DoItFlag = True;
  250.                                     LoopFlag = False;
  251.                                 }
  252.                             else if (KeyPress == eCancelKey)
  253.                                 {
  254.                                     FlashButton(Window->CancelButton);
  255.                                     DoItFlag = False;
  256.                                     LoopFlag = False;
  257.                                 }
  258.                             else
  259.                                 {
  260.                                     TextEditDoKeyPressed(Window->EditBox,KeyPress,Modifiers);
  261.                                 }
  262.                             break;
  263.                         case eMouseDown:
  264.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  265.                                 {
  266.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  267.                                         {
  268.                                             DoItFlag = True;
  269.                                             LoopFlag = False;
  270.                                         }
  271.                                 }
  272.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  273.                                 {
  274.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  275.                                         {
  276.                                             DoItFlag = False;
  277.                                             LoopFlag = False;
  278.                                         }
  279.                                 }
  280.                             else if (TextEditHitTest(Window->EditBox,X,Y))
  281.                                 {
  282.                                     TextEditDoMouseDown(Window->EditBox,X,Y,Modifiers);
  283.                                 }
  284.                             break;
  285.                     }
  286.             }
  287.         ERROR((DoItFlag != True) && (DoItFlag != False),PRERR(ForceAbort,
  288.             "CommandDialogOneParam:  DoItFlag is neither true nor false"));
  289.  
  290.         ReturnValue = False;
  291.  
  292.         if (DoItFlag)
  293.             {
  294.                 MyBoolean                    ErrorNoMemory;
  295.  
  296.                 ErrorNoMemory = False;
  297.  
  298.                 if (TextEditDoesItNeedToBeSaved(Window->EditBox))
  299.                     {
  300.                         StringTemp = TextEditGetRawData(Window->EditBox,SYSTEMLINEFEED);
  301.                         if (StringTemp == NIL)
  302.                             {
  303.                                 ErrorNoMemory = True;
  304.                             }
  305.                          else
  306.                             {
  307.                                 *DataInOut = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  308.                                 ReleasePtr(StringTemp);
  309.                                 ReturnValue = True;
  310.                             }
  311.                     }
  312.  
  313.                 if (ErrorNoMemory)
  314.                     {
  315.                         AlertHalt("There was not enough memory available to save all of "
  316.                             "the attributes.",NIL);
  317.                     }
  318.             }
  319.  
  320.         DisposeSimpleButton(Window->OKButton);
  321.         DisposeSimpleButton(Window->CancelButton);
  322.         DisposeTextEdit(Window->EditBox);
  323.         KillWindow(Window->ScreenID);
  324.         ReleasePtr((char*)Window);
  325.  
  326.         return ReturnValue;
  327.     }
  328.